<?php

namespace app\common\service;


class HostService extends Base
{
    public $stateOpen = 1;//正在服务的状态
    /***
     * @name 列表
     * @param $params
     * @param $limit
     * @return array
     * @author wx
     * @date 2019/10/24 10:21
     */
    function list($params, $limit)
    {
        $db = db('host_service');

        if (isset($params['name']) && !empty($params['name'])) {
            $db->where('name', 'like', '%' . $params['name'] . '%');
        }
        if (isset($params['status']) && !empty($params['status'])) {
            $db->where('status', $params['status']);
        }
        if (isset($params['description']) && !empty($params['description'])) {
            $db->where('description', 'like', '%' . $params['description'] . '%');
        }
        if (isset($params['type']) && !empty($params['type'])) {
            $service_id_arr = db('host_service_type_relation')
                ->field('service_id')->where('type_id', $params['type'])
                ->column('service_id');
            $db->where('id', 'in', $service_id_arr);
        }


        $field = 'id,name,status,description';
        $data = $db->field($field)->order('id desc')->paginate($limit)->toArray();

        return $data;
    }

    /**
     * @name 添加
     * @param $addData
     * @return int|string
     * @author wx
     * @date 2019/10/24 10:55
     */
    function add($addData)
    {
        return db('host_service')->insertGetId($addData);
    }

    /**
     * @name 查询
     * @param $condition
     * @author wx
     * @date 2019/10/24 10:56
     */
    function findData($condition)
    {
        return db('host_service')->where($condition)->find();
    }

    /**
     * @name 修改
     * @param $condition
     * @param $updateData
     * @return int|string
     * @author wx
     * @date 2019/10/24 11:13
     */
    function modify($condition, $updateData)
    {
        return db('host_service')->where($condition)->update($updateData);
    }

    function del($condition)
    {
        return db('host_service')->where($condition)->delete();
    }

    /**
     * @name 查询所有
     * @param $field
     * @author wx
     * @date 2019/10/24 16:30
     */
    function findAll($field = '*')
    {
        return db('host_service')->field($field)->select();
    }

    /**
     * @name: 展示服务商
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-04-01 14:22:57
     */
    function showService ($id_a = [], $field = '', $index = '')
    {
        $server = db('HostService')->alias('hs');

        empty($id_a) ? : $server->whereIn('id', $id_a);

        empty($field) ? : $server->field($field);

        $serviceData = empty($index) ? $server->select() : $server->column($field, $index);

        return $serviceData;
    }

    /**
     * @name: 展示服务商
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-30 11:23:03
     */
    function extraService ($type)
    {
        $server = db('HostService')->alias('hs')->field('hs.id,hs.name,hs.contact,hs.status');
        $server->join('HostServiceTypeRelation hstr', 'hstr.service_id = hs.id')->where('hstr.type_id', $type);
        $return = $server->select();
        $msg = '(合作已终止)';
        $return = array_map(function ($v) use ($msg){
            if($v['status'] == 2)
            {
                $v['name'] .= $msg;
                $v['contact'] .= $msg;
            }
            return $v;
        }, $return);

        return $return;
    }
}